package com.example.mobile2030606148tdh0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox ChkboxMainYinqing;
    private CheckBox ChkboxMainBoli;
    private CheckBox ChkboxMainFangxiangpan;
    private Button BtnMainBuy;
    private TextView tvMainTip;
    private TextView TvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ChkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    TvMainResult.setText("选中:"+buttonView.getText().toString());
                }else{
                    TvMainResult.setText("取消选中:"+buttonView.getText().toString());
                }
            }
        });
        ChkboxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    TvMainResult.setText("选中:"+buttonView.getText().toString());
                }else{
                    TvMainResult.setText("取消选中:"+buttonView.getText().toString());
                }


            }
        });
        BtnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中:";
                if (ChkboxMainYinqing.isChecked()){
                    msg+="\n"+ChkboxMainYinqing.getText().toString();
                }

                if (ChkboxMainBoli.isChecked()){
                    msg+="\n"+ChkboxMainBoli.getText().toString();
                }
                if (ChkboxMainFangxiangpan.isChecked()){
                    msg+="\n"+ChkboxMainFangxiangpan.getText().toString();
                }
                TvMainResult.setText(msg);
            }

        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        ChkboxMainYinqing = (CheckBox) findViewById(R.id.Chkbox_main_yinqing);
        ChkboxMainBoli = (CheckBox) findViewById(R.id.Chkbox_main_boli);
        ChkboxMainFangxiangpan = (CheckBox) findViewById(R.id.Chkbox_main_fangxiangpan);
        BtnMainBuy = (Button) findViewById(R.id.Btn_main_buy);
        tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
        TvMainResult = (TextView) findViewById(R.id.Tv_main_result);
    }
}